package gui.search;

import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import framework.AnnuaireCocola;
import framework.classification.library.AbstractClassfication;
import gui.controller.Controller;
import gui.utils.DegradeVerticalPanel;
import gui.utils.JCoolButton;
import gui.utils.JCoolToggleButton;
import gui.utils.JRoundButton;
import gui.utils.JRoundDegradePanel;
import gui.utils.MyAutoCompletion;
import gui.utils.RoundedBorder;






public class AdvencedSearch extends JRoundDegradePanel implements ActionListener {

	private AbstractClassfication classification;
	private MyAutoCompletion field;
	private JComboBox combo;
	private JCoolButton button;
	private Controller controller;

	public static String SEARCH = "search";

	public AdvencedSearch(AbstractClassfication classfication){
		super();
		this.classification = classfication;
		this.setLayout(new BorderLayout());
		this.add(createComponent(),BorderLayout.CENTER);
		setColor1(new Color(230,230,230));
		setColor2(new Color(175,175,175));
		updateComboBox();
		
	}
	
	public AdvencedSearch(AbstractClassfication classfication,Controller controller){
		super();
		this.controller = controller;
		this.classification = classfication;
		this.setLayout(new BorderLayout());
		this.add(createComponent(),BorderLayout.CENTER);
		setColor1(new Color(230,230,230));
		setColor2(new Color(175,175,175));
		updateComboBox();


	}

	public MyAutoCompletion getAutoCompletion(){return field;}
	private JComponent createComponent(){
		Box mainBox = Box.createHorizontalBox();

		ImageIcon icon = new ImageIcon("icons/search2.png");
		Object[] o = {"component","interface","function"};

		field = new MyAutoCompletion(new ArrayList<String>(),(JFrame) this.getParent());
		//field.setBorder(BorderFactory.createEmptyBorder());
		field.setPreferredSize(new Dimension(700,22));
		field.setMaximumSize(new Dimension(700,22));


		combo = new JComboBox(o);
		combo.setBackground(Color.WHITE);
		//combo.setBorder(new RoundedBorder(Color.YELLOW,25,25));
		combo.setPreferredSize(new Dimension(120,22));
		combo.setMaximumSize(new Dimension(120,22));
		combo.addActionListener(this);
		Box hor = Box.createHorizontalBox();

		hor.add(field);
		hor.add(combo);

		button = new JCoolButton("search",new Color(0,120,160),new Color(0,100,140));
		button.setActionCommand(SEARCH);
		button.addActionListener(controller);

		Box vert = Box.createVerticalBox();

		mainBox.add(Box.createHorizontalGlue());
		mainBox.add(new JLabel(icon));
		mainBox.add(Box.createHorizontalStrut(15));
		mainBox.add(hor);
		mainBox.add(Box.createHorizontalStrut(25));
		mainBox.add(button);
		mainBox.add(Box.createHorizontalGlue());

		vert.add(mainBox);
		vert.add(Box.createVerticalStrut(15));


		return vert;
	}
	public void updateComboBox() {
		if (combo.getSelectedItem().equals("component")) {

			field.changeData(getListComponent());
			

		}
		if (combo.getSelectedItem().equals("interface")) {

			field.changeData(getListInterface());
		}
		if (combo.getSelectedItem().equals("function")) {

			field.changeData(getListFunction());
		}
	}


	private Vector<String> getListComponent(){

		Vector<String> vect = new Vector<String>();
		vect.addAll(classification.getComponentsNames().keySet());


		return vect;
	}
	private Vector<String> getListInterface(){

		Vector<String> vect = new Vector<String>();
		vect.addAll(classification.getInterfacesNames().keySet());
		return vect;
	}
	private Vector<String> getListFunction(){

		Vector<String> vect = new Vector<String>();
		vect.addAll(classification.getFunctionRequired().keySet());
		vect.removeAll(classification.getFunctionsProvided().keySet());
		vect.addAll(classification.getFunctionsProvided().keySet());
		return vect;
	}
	
	public void actionPerformed(ActionEvent arg0) {
		updateComboBox();
	}

	public void setController(Controller c){controller = c;}
	public String getCurrentType(){
		return (String) combo.getSelectedItem();
	}
	public String getCurrentSearch(){return (String) field.getSelectedItem();}
}
